package com.softdev.controller;

import com.softdev.common.R;
import com.softdev.pojo.Project;
import com.softdev.repo.ProjectRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@Slf4j
@Api(tags = "通用操作")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private ProjectRepository projectRepository;

    @Value("${outsource.path}")
    private String basePath;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) {

        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();
        String suffix = null;
        if (originalFileName != null) {
            suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
        }

        String fileName = UUID.randomUUID() + suffix;

        File dir = new File(basePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        try {
            file.transferTo(new File(basePath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.success(fileName);

    }

    @ApiOperation("下载图片")
    @GetMapping("/download")
    public void download(String name , HttpServletResponse response) {

        try {

            FileInputStream inputStream = new FileInputStream(basePath + name);

            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes , 0 , len);
                outputStream.flush();
            }

            outputStream.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @ApiOperation("报表下载")
    @GetMapping("/report")
    public void report(Long projectId, HttpServletResponse response) throws IOException {

        Project project = projectRepository.findByIdWithLazyProperties(projectId);

        // 创建一个新的 Word 文档对象
        XWPFDocument document = new XWPFDocument();

        // 创建段落对象并添加文本内容
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("Project Name: " + project.getName());
        run.addBreak();
        run.setText("Project Type: " + project.getType());
        // 添加其他项目属性的信息

        // 设置响应头，告诉浏览器下载文件
        response.setHeader("Content-Disposition", "attachment; filename=project.docx");
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");

        // 获取输出流并将文档写入输出流
        OutputStream outputStream = response.getOutputStream();
        document.write(outputStream);
        outputStream.close();

        // 关闭文档
        document.close();
    }

}
